package com.zhu.service.admin.controller.web;


import com.zhu.commons.utils.JwtInfo;
import com.zhu.service.admin.pojo.vo.MemberVo;
import com.zhu.service.admin.utils.JwtUtils;
import com.zhu.commons.utils.R;
import com.zhu.service.admin.pojo.vo.LoginVo;
import com.zhu.service.admin.pojo.vo.RegisterVo;
import com.zhu.service.admin.service.IMemberService;
import com.zhu.service.base.handler.error.LoginErrorException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author zhu
 * @since 2022-08-17
 */
@RestController
@RequestMapping("api/member")
@Api(value = "会员账号管理")
public class MemberController {

    @Autowired
    private IMemberService memberService;
    @PostMapping("/register")
    @ApiOperation(value = "会员注册")
    public R register(@RequestBody RegisterVo registerVo){
        String message=memberService.register(registerVo);
        return R.ok().message(message);
    }


    @PostMapping("/login")
    @ApiOperation(value = "用户登录(jwt)")
    public R register(@RequestBody LoginVo loginVo) throws Exception {
      Map<String,Object> map =  memberService.login(loginVo);
      String token=  (String)map.get("success");
      String message=  (String)map.get("error");
      if(token!=null){
          return R.ok().data("token",token);
      }
      return R.error().message(message);
    }
    @PostMapping("/getUserInfoByToken")
    @ApiOperation(value = "获得用户信息")
    public R getUserInfoByToken(HttpServletRequest request)throws LoginErrorException {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        if(jwtInfo!=null){
            return R.ok().data("userInfo",jwtInfo);
        }
        return R.error().code(101).message("登陆过期，请重新登录！");
    }
    @GetMapping("/test")
    public String test(){
        return "hello";

    }

    @GetMapping("/order/getMemberById/{memberId}")
    @ApiOperation("获取会员信息")
    public MemberVo getMember(@PathVariable String memberId){
      MemberVo memberVo=  memberService.getMember(memberId);
      return memberVo;
    }


}
